import {
	addBook,
	updateUserDefaultBook
} from "./book";

// 注册
const registerMember = (username, password) => {
	return new Promise(resolve => {
		vk.userCenter.register({
			data: {
				username,
				password
			},
			complete(res) {
				console.log("registerMember", res)
				// 注册成功后的逻辑
				if (res.result.code !== 0) {
					resolve(false)
				} else {
					// 添加默认账本
					addBook('默认账本', true).then(id => {
						if (id !== "") {
							updateUserDefaultBook(id).then(_res => {
								resolve(_res)
							})
						} else {
							resolve(false)
						}
					})

				}
			}
		});
	})
}


// 登录
const loginMember = (username, password) => {
	return new Promise(resolve => {
		vk.userCenter.login({
			data: {
				username,
				password
			},
			complete: (res) => {
				// 注册成功后的逻辑
				if (res.result.code === 0) {
					resolve(true)
				} else {
					resolve(false)
				}
			}
		});
	})
}


export {
	registerMember,
	loginMember,
}